<?php

App::uses('AppController', 'Controller');

class ProizvodnjaController extends AppController {
	
	public $uses = array('Production', 'Client', 'ProductPrice', 'Tax', 'Data', 
						 'Carrier', 'Driver', 'Vehicle');
	public $helpers = array('Production');
	
	public $paginate = array(
		'limit' => ITEMS_PER_PAGE,
	    'recursive' => 2,
		'order' => 'Production.date DESC'
	);
	
	private function _setCommonData() {
		$products   = $this->Production->Product->getOptions();
		$clients    = $this->Client->find('list');
		$resTaxes   = $this->Tax->find('all', array('order'=>'id DESC'));
		$taxes 	    = Set::Combine($resTaxes, '{n}.Tax.id', '{n}.Tax.value');
		$tax_values = Set::combine($resTaxes, '{n}.Tax.id', array(
			'{0} ({1}%)', '{n}.Tax.name', '{n}.Tax.value'
		));
		$carriers   = $this->Carrier->find('list');
		
		$this->set(compact('products', 'clients', 'taxes', 'tax_values', 'carriers'));
	}
	
    public function index() {
		
	}
	
	public function cenovnik($date=null) {
		$this->autoRender = false;
		$date = date('Y-m-d', strtotime($date));
		
		echo json_encode($this->Production->getPricelist($date));
	}
	
	public function porez($date=null) {
		$this->autoRender = false;
		$date = $date ? $date : date('y-m-d');
		echo $this->Tax->field('value', array('Tax.effective_date <='=>$date), 'Tax.effective_date DESC');
		
	}
	
	public function povracaj() {
		$filter = $this->params->query;
		$conditions = array('AND' => array('Production.type' => PRODUCTION_TYPE_POVRACAJ) );
		if (!empty($filter)) {
			if (!empty($filter['keyword'])) {
				$filter['keyword'] = trim($filter['keyword']);
				$kword = $filter['keyword'];
				$conditions['OR'] = array(
					'Store.name LIKE' => "%$kword%",
					'Store.address LIKE' => "%$kword%",
					'Store.township LIKE' => "%$kword%",
					'Store.city LIKE' => "%$kword%",
					'Production.number LIKE' => "%$kword%"
				);
			}
			if (!empty($filter['date_from'])) {
				$conditions['AND']['Production.date >='] = $filter['date_from'];
			}
			if (!empty($filter['date_to'])) {
				$conditions['AND']['Production.date <='] = $filter['date_to'];
			}
			$this->set('filter', $filter);
		}
		$data = $this->paginate('Production', $conditions);
		$this->set("data", $data);
	}

	public function otprema() {
		$filter = $this->params->query;
		$conditions = array('AND' => array('Production.type' => PRODUCTION_TYPE_OTPREMA) );
		if (!empty($filter)) {
			if (!empty($filter['keyword'])) {
				$filter['keyword'] = trim($filter['keyword']);
				$kword = $filter['keyword'];
				$conditions['OR'] = array(
					'Store.name LIKE' => "%$kword%",
					'Store.address LIKE' => "%$kword%",
					'Store.township LIKE' => "%$kword%",
					'Store.city LIKE' => "%$kword%",
                    'Production.number LIKE' => "%$kword%"
				);
			}
			if (!empty($filter['date_from'])) {
				$conditions['AND']['Production.date >='] = $filter['date_from'];
			}
			if (!empty($filter['date_to'])) {
				$conditions['AND']['Production.date <='] = $filter['date_to'];
			}
			$this->set('filter', $filter);
		}
        $data = $this->paginate('Production', $conditions);
        $this->set("data", $data);
	}
	
	public function nova_otprema() {
		$this->_setCommonData();
		$this->set('tax', $this->Tax->getForDate());
		$this->set("data", $this->Production->create(array('Production'=>array('type'=>PRODUCTION_TYPE_OTPREMA))));
		$this->render("form");
	}
	
	public function novi_povracaj() {
		$this->_setCommonData();
		$this->set('tax', $this->Tax->getForDate());
		$this->set("data", $this->Production->create(array('Production'=>array('type'=>PRODUCTION_TYPE_POVRACAJ))));
		$this->render("form");
	}
	
	public function izmeni($id) {
		$data = $this->Production->findById($id);
		if($data) {
			$this->_setCommonData();
			$this->set('data', $data);
		    $this->set('tax', $this->Tax->getForDate($data['Production']['date']));
			return $this->render("form");
		}
		
		$this->set('tax', $this->Tax->getForDate(date('y-m-d')));
		$this->redirect("index");
	}
	
	public function sacuvaj() {
		if(!empty($this->data)) {
			$data = $this->data;
			
			$this->Production->create($data);
			if($this->Production->validates()) {
				
				if($this->Production->id > 0){
					$this->Production->ProductionProduct->deleteAll(array('production_id' => $this->Production->id));
				}
				
				if($this->Production->saveAll($data)) {
					$this->setFlash("Sačuvano.", "ok");
					return $this->redirect('/proizvodnja/izmeni/' . $this->Production->id);
				}else{
				    if($data['Production']['store_id'] && empty($data['Store'])) {
				        $store = $this->Production->Store->findById($data['Production']['store_id'], array('Store.*'));
				        $data['Store'] = $store['Store'];
				    }
				    
					$this->setFlash("Otpremnica nije sačuvana.", "error");
					$this->_setCommonData();
					$this->set('tax', $this->Tax->getForDate($data['Production']['date']));
					$this->set('data', $data);
					return $this->render('form');
				}
			}else{
				$this->_setCommonData();
				$this->set('tax', $this->Tax->getForDate($data['Production']['date']));
				$this->set('data', $data);
				return $this->render('form');
			}
		}
		$this->redirect('index');
	}
	
	public function obrisi($id) {
	    $production = $this->Production->findById($id);
	    if($production) {
	        $type = $production['Production']['type'];
	        $this->Production->delete($id);
			$this->setFlash("Brisanje izvršeno.");
			
			$action = $type=='povraćaj' ? 'povracaj' : 'otprema';
			$this->redirect("/proizvodnja/$action");
		}
		$this->redirect("index");
	}
	
	public function pregled($id) {
	    $data = $this->Production->find('first', array(
	        'conditions' => array( 'Production.id' => $id ),
	        'recursive' => 2
	    ));
	    if ($data) {
	       $this->_setCommonData();
	       $this->set('data', $data);
		   $this->set('company_data', $this->Data->getCompanyData(true));
	       $this->layout = 'print'; 
	    }else{
	        $this->redirect("index");
	    }
	}
	
	public function prevoznik_data($carrier_id) {
		$this->autoRender = false;
		$ret = array('vehicles'=>array(), 'drivers'=>array());
		$ret['vehicles'] = $this->Vehicle->find('list', array(
			'fields'	 => array('id', 'plates'),
			'conditions' => array('carrier_id' => $carrier_id)
		));
		$ret['drivers'] = $this->Driver->find('list', array(
			'conditions' => array('carrier_id' => $carrier_id)
		));
		
		echo json_encode($ret);
	}
}

?>